package cn.longg.mapper;

import cn.longg.entity.User;
import org.apache.ibatis.annotations.*;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

public interface UserMapper extends Mapper<User> {

    @Update("update user set password = #{password} where email = #{email}")
    public int updatePassword(User user);

    @Select("select count(1) from userfocus where user_focus_id = #{id}")
    int getFansNum(Integer id);

    @SelectProvider(value = UserMapperSqlProvider.class,method = "selectAllByName")
    List<User> selectAllByName(String name);

    @Update("update user set look = look + 1 where id = #{id}")
    void lookIncr(Integer id);

    @Select("select user_focus_id from userfocus where user_id = #{id}")
    List<Integer> selectFocusUser(Integer id);

    @Insert("insert into userfocus values(#{id2},#{id1})")
    int focus(@Param("id1") Integer id,@Param("id2") Integer id1);

    @Delete("delete from userfocus where user_id = #{id2} and user_focus_id = #{id1}")
    int unfocus(@Param("id1") Integer id,@Param("id2") Integer id1);

    @Select("select * from user where EXISTS(select user_focus_id from userfocus where user_id = #{id} and user_focus_id = id)")
    List<User> selectFocusUserList(Integer id);

//    @Select("select * from user where EXISTS(select 1 from favorite where a_id = #{articleId} and u_id = id)")
    @Select("select * from user where id in (select user_focus_id from userfocus where user_id = #{userId}) " +
            "and id in (select u_id from favorite where a_id = #{articleId})")
    List<User> whoFavorite(@Param("userId") int userId,@Param("articleId") int articleId);

    @Select("select COUNT(1) from favorite where a_id = #{articleId} and u_id = #{userId} ")
    int ifFavArticle(@Param("articleId")int articleId, @Param("userId")Integer id);

    @Select("select * from user where dept_id = #{deptId}")
    List<User> selectByDeptId(int deptId);
}